<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="名称" prop="userName">
            <el-input
              v-model="queryParams.typeName"
              placeholder="请输入名称"
              clearable
              size="small"
              style="width: 180px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="类型" prop="status">
            <el-select
              v-model="queryParams.type"
              placeholder="选择类型"
              clearable
              size="small"
              style="width: 180px"
            >
              <el-option v-for="item in floristList" :key="item.id" :label="item.name" :value="item.id" />
              <!-- <el-option :key="1" label="停用" :value="1" /> -->
            </el-select>
            <el-button
                style="margin-left: 50px;"
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
              >搜索</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              style="float: right;"
              type="primary"
              plain
              icon="el-icon-save"
              size="mini"
              @click="open = true"
              >添加</el-button>
          </el-col>
        </el-row>

        <el-table :data="typeList" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column prop="name" label="分类" align="center" />
          <el-table-column prop="companyName" label="属于" align="left" />
          <!-- <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value=0
                :inactive-value=1
                @change="handleStatusChange(scope.row)"
              />
            </template>
          </el-table-column> -->

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="queryType"
      @size-change="queryType"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入名称"
                maxlength="30"
              />
            </el-form-item>
            <el-form-item label="类型" prop="status">
                <el-select
                v-model="form.type"
                placeholder="选择类型"
                clearable
                size="small"
                style="width: 180px"
                >
                <el-option v-for="item in floristList" :key="item.id" :label="item.name" :value="item.id" />
                <!-- <el-option :key="1" label="停用" :value="1" /> -->
                </el-select>
          </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getToken } from '@/utils/auth'

// import  {
//     listflower,typeFlower,delFlower,uploadImg,addFlower,listFlorist,addFlorist,delFlorist
// } from '@/api/flower/flower.js';

import { typeFlower,listFlowerpotCompany,addFlowerpotType,delFlowerpotType } from '@/api/flowerpot/flowerpot.js'

import {
  listAllRole
}
from '@/api/system/role'
import { 
  queryStudyAll 
} from "@/api/content/tag";
export default {
  name: 'User',
  data() {
    return {
      //学习方向集合
      studyList:[],
      exportLoading:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: undefined,
        type: undefined,
      },
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        // userName: [
        //   { required: true, message: '用户名称不能为空', trigger: 'blur' },
        //   {
        //     min: 2,
        //     max: 20,
        //     message: '用户名称长度必须介于 2 和 20 之间',
        //     trigger: 'blur'
        //   }
        // ],
        headNumber: [
          { required: true, message: '学号只能为8位数', trigger: 'blur',max:8,min:8 }
        ],
        password: [
          { min: 1,
            max: 1,
            required: true, 
            message: '用户密码必须有英文加数字组成', 
            trigger: 'blur' 
          },
        ],

      },
      // 角色选项
      roleOptions: [],
      fileList:[],
      // 显示搜索条件
      showSearch: true,
      typeList:[],
      // 遮罩层
      loading: true,
      // 用户表格数据
      floristList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {
        name:'',
        type:undefined,
      }
    }
  },
  watch: {},
  created() {
    this.queryType()
    this.getfFlorist()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.queryType()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    queryType(){
        typeFlower(this.queryParams).then((response) => {
            this.typeList = response.rows
            this.total = response.total
        })
    },
    getfFlorist() {
        setTimeout(() => {
            this.queryParams.pageSize = 1000
            listFlowerpotCompany(this.queryParams).then((response) => {
                this.floristList = response.rows
                this.queryParams.pageSize = 10
            })
        },500)
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        type: undefined,
      }
      this.resetForm('form')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除编号为"' + row.name + '"的数据项？')
        .then(function() {
          return delFlowerpotType(ids)
        })
        .then(() => {
          this.queryType()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.$modal.msgSuccess('修改成功')
            // updateUser(this.form).then((response) => {
            //   this.$modal.msgSuccess('修改成功')
            //   this.open = false
            //   this.getList()
            // })
          } else {
            //添加走这段逻辑
            addFlowerpotType(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.reset()
              this.queryType()
            })
          }
        }
      })
    }
  }
}
</script>
<style scoped>
div .upload-demo {
  /* padding-left: 80px; */
}
.el-col .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;

  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
